package com.bawei.home.viewmodel

import androidx.lifecycle.viewModelScope
import com.bawei.common.vm.BaseViewModel
import com.bawei.home.intent.HomeViewIntent
import com.bawei.home.model.HomeRepository
import com.bawei.home.uistate.HomeUiState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * Author: Jason Zhang
 * Date: 2024/3/1
 * Desc:
 */

@HiltViewModel
class HomeViewModel @Inject constructor(private val repository: HomeRepository): BaseViewModel<HomeViewIntent>(){

    private val _uiState = MutableStateFlow<HomeUiState>(HomeUiState.LoadEmpty)
    val uiState: StateFlow<HomeUiState> = _uiState.asStateFlow()

    override fun dispatchIntent(intent: HomeViewIntent) {
       when(intent){
           is HomeViewIntent.LoadVideoType -> loadSimpleType()
           is HomeViewIntent.LoadRecommendSimpleVideoList -> loadRecommendSimpleVideoList(intent.page, intent.pageSize)
           is HomeViewIntent.LoadSimpleVideoListById -> loadSimpleVideoById(intent.channelId, intent.page, intent.pageSize)
       }
    }

    private fun loadSimpleType() {
        viewModelScope.launch {
            try {
                //对象解构，注意字段顺序必须一致
                val (code, data, msg) = repository.getSimpleType()
                if (code == 0 && data.isNotEmpty()) {
                    _uiState.value = HomeUiState.LoadSimpleTypeSuccess(data)
                } else {
                    _uiState.value = HomeUiState.LoadSimpleTypeFail(msg)
                }
            } catch (e: Exception) {
                _uiState.value = HomeUiState.LoadSimpleTypeFail("网络请求失败！")
            } finally {
                this.cancel()
            }
        }
    }

     fun loadRecommendSimpleVideoList(page:Int, pageSize: Int) {
        viewModelScope.launch {
            try {
                val (code, data, msg) = repository.getRecommendVideo(page, pageSize)
                if(code == 0 && data.isNotEmpty()){
                    _uiState.value = HomeUiState.LoadSimpleVideoSuccess(data)
                }else{
                    _uiState.value = HomeUiState.LoadSimpleTypeFail(msg)
                }
            } catch (e: Exception){
                _uiState.value = HomeUiState.LoadSimpleTypeFail("网络请求失败！")
            } finally {
                this.cancel()
            }
        }
    }

    fun loadSimpleVideoById(channelId: String, page:Int, pageSize: Int){
        viewModelScope.launch {
            try {
                repository.getVideoByChannelId(channelId, page, pageSize).collect{ list ->
                    if(list.isNotEmpty()){
                        _uiState.value = HomeUiState.LoadSimpleVideoSuccess(list)
                    }else{
                        _uiState.value = HomeUiState.LoadSimpleVideoFail("没有数据！")
                    }
                }
            } catch (e: Exception){
                _uiState.value = HomeUiState.LoadSimpleTypeFail("网络请求失败！")
            } finally {
                this.cancel()
            }
        }

    }


}